//
//  YZMForgetWithdrawPwdViewController.m
//  YZMicroStock
//
//  Created by Lv.Dongdong on 16/4/20.
//  Copyright © 2016年 cqjr. All rights reserved.
//

#import "YZMForgetWithdrawPwdViewController.h"
#import "YZMHintInputView.h"
#import "YZMAuthCodeView.h"
#import "YZGeneralResultModel.h"
#import "YZClient+Account.h"

@interface YZMForgetWithdrawPwdViewController () <UITextFieldDelegate>

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) YZMAuthCodeView *authCodeView;
@property (nonatomic, strong) YZMHintInputView *passwordView;
@property (nonatomic, strong) YZMHintInputView *ConfirmPwdView;

@end

@implementation YZMForgetWithdrawPwdViewController {
    
    UIButton *_resetPwdButton;
}

- (void)dealloc {
    [self.authCodeView.timer invalidate];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showKeyboard:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hideKeyboard:) name:UIKeyboardWillHideNotification object:nil];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:YES];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
    [self.authCodeView.timer invalidate];
}

#pragma mark -- KeyboardNotification
- (void)showKeyboard:(NSNotification *)noti {
    NSDictionary *dic = noti.userInfo;
    //键盘弹出后的位置
    CGRect endFrame = [dic[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    [self.scrollView mas_updateConstraints:^(MASConstraintMaker *make){
        make.bottom.equalTo(self.view).offset(-endFrame.size.height);
    }];
}

- (void)hideKeyboard:(NSNotification *)noti {
    
    [self.scrollView mas_updateConstraints:^(MASConstraintMaker *make){
        make.bottom.equalTo(self.view);
    }];
}

#pragma mark -- init

- (id)init {
    if (self = [super init]) {
        self.view.backgroundColor = [YZMAppStyle appBackgroundColor];
        self.title = @"找回提现密码";
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createObjects];
}

#pragma mark -- createObjects
- (void)createObjects {
    WS(ws)
    
    self.scrollView = [[UIScrollView alloc] init];
    CGSize contentSize = CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height - 64);
    if (iPhone4 || iPhone5) {
        self.scrollView.showsHorizontalScrollIndicator = NO;
        self.scrollView.showsVerticalScrollIndicator = NO;
        self.scrollView.contentSize = contentSize;
        self.scrollView.bounces = NO;
    }
    [self.view addSubview:self.scrollView];
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(ws.view);
    }];
    
    self.authCodeView = [[YZMAuthCodeView alloc] initWithScene:SEND_CODE_SCENE_FOR_FORGET_WITHDRAW];
    [self.scrollView addSubview:self.authCodeView];
    [self.authCodeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(ws.scrollView);
        make.width.equalTo(ws.scrollView);
        make.left.equalTo(ws.scrollView.mas_left);
        make.height.mas_equalTo(102);
    }];
    self.authCodeView.phoneTextField.delegate = self;
    self.authCodeView.textField.delegate = self;
    [self.authCodeView.phoneTextField addTarget:self action:@selector(textFieldchange:) forControlEvents:UIControlEventEditingChanged];
    [self.authCodeView.textField addTarget:self action:@selector(textFieldchange:) forControlEvents:UIControlEventEditingChanged];
    
    self.passwordView = [[YZMHintInputView alloc] init];
    [self.scrollView addSubview:self.passwordView];
    [self.passwordView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(ws.authCodeView.mas_bottom).with.offset(ControlSpacing15);
        make.width.equalTo(ws.view);
        make.height.mas_equalTo(InputViewHeight);
        make.left.equalTo(ws.scrollView.mas_left);
    }];
    self.passwordView.inputLabel.text = @"提现密码";
    self.passwordView.textField.placeholder = @"由6位纯数字组成";
    self.passwordView.textField.keyboardType = UIKeyboardTypeNumberPad;
    self.passwordView.textField.returnKeyType = UIReturnKeyNext;
    self.passwordView.textField.secureTextEntry = YES;
    self.passwordView.textField.delegate = self;
    [self.passwordView.textField addTarget:self action:@selector(textFieldchange:) forControlEvents:UIControlEventEditingChanged];
    self.passwordView.lineTopView.hidden=NO;
    
    self.ConfirmPwdView = [[YZMHintInputView alloc] init];
    [self.scrollView addSubview:self.ConfirmPwdView];
    [self.ConfirmPwdView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(ws.scrollView.mas_left);
        make.top.equalTo(ws.passwordView.mas_bottom);
        make.width.height.equalTo(ws.passwordView);
    }];
    
    self.ConfirmPwdView.inputLabel.text = @"确认密码";
    self.ConfirmPwdView.textField.placeholder = @"请再次确认提现密码";
    self.ConfirmPwdView.textField.keyboardType = UIKeyboardTypeNumberPad;
    self.ConfirmPwdView.textField.returnKeyType = UIReturnKeyNext;
    self.ConfirmPwdView.textField.secureTextEntry = YES;
    self.ConfirmPwdView.lineBottomView.hidden=NO;

    
    self.ConfirmPwdView.textField.returnKeyType = UIReturnKeyDone;
    self.ConfirmPwdView.textField.delegate = self;
    [self.ConfirmPwdView.textField addTarget:self action:@selector(textFieldchange:) forControlEvents:UIControlEventEditingChanged];
    
    [self.passwordView.textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_ConfirmPwdView.textField.mas_left);
    }];
    
    self.passwordView.lineBottomView.hidden = YES;
    [self.ConfirmPwdView updateLineIndentationMethod:self.ConfirmPwdView.lineTopView enumeration:LINE_INDENT_SCENE_LEFT indentValue:ControlSpacing15];
    
    _resetPwdButton = [YZMAppStyle NewsubmitButton];
    [_resetPwdButton setTitle:@"确定" forState:UIControlStateNormal];
    [_resetPwdButton addTarget:self action:@selector(confirmButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    _resetPwdButton.enabled = NO;
    [self.scrollView addSubview:_resetPwdButton];
    [_resetPwdButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(ws.view).with.offset(ControlSpacing20);
        make.right.equalTo(ws.view).with.offset(-ControlSpacing20);
        make.top.equalTo(ws.ConfirmPwdView.mas_bottom).with.offset(ControlSpacing30);
    }];
    
    self.authCodeView.voiceAuthEvent = ^(){
        [ws resignAllFirstResponder];
    };
}

#pragma mark -- buttonEvents

- (void)confirmButtonPressed {
    [self resignAllFirstResponder];
    
    if (![self checkInputContent:self.authCodeView.phoneTextField] ||
        ![self checkInputContent:self.authCodeView.textField] ||
        ![self checkInputContent:self.passwordView.textField] ||
        ![self checkInputContent:self.ConfirmPwdView.textField]) {
        return;
    }
    
    if (![self.passwordView.textField.text isEqualToString:self.ConfirmPwdView.textField.text]) {
        [MBProgressHUD showMessage: @"两次密码输入不一致"];
        return;
    }
    
    [YZClient forget_resetWithdrawPwdWithPwd: self.passwordView.textField.text andCheckCode:self.authCodeView.textField.text success:^(NSURLSessionDataTask *task,id responseObject) {
        YZGeneralResultModel *topModel = responseObject;
        if (topModel.success) {
            [MBProgressHUD showMessage: @"提现密码重置成功"];
            
            if ([YZMHCMInstance instanceWithControllerName:@"changeWdPwd"]) {
                
                [YZMHCMInstance popViewControllerBeforeControllerName:@"changeWdPwd" animated:YES];
            }else{
                [YZMHCMInstance popViewController:nil animated:YES];
            }
            
            
        } else
        {
            [topModel showMessage];
        }
    } failure:^(NSError *error) {
        [error yz_showMessage];
    }];
    
}

- (BOOL)checkInputContent:(UITextField *)textField {
    
    if (textField == self.authCodeView.phoneTextField) {
        
        if ([textField.text yz_isValidPhoneString]) {
            BOOL ynFlat = [self phoneCheck];
            if (!ynFlat || [textField.text length] < 11) {
                [MBProgressHUD showMessage:@"请输入正确的手机号码"];
                return NO;
            }
            
            self.authCodeView.phoneStr = textField.text;
            return YES;
        } else {
            [self registerValidateFail:@"请输入正确的手机号码"];
            return NO;
        }
        
    }
    
    if (textField == self.authCodeView.textField) {
        if (!self.authCodeView.getAuthFlag) {
            [self registerValidateFail:@"请先获取验证码"];
            return NO;
        }
        if ([self.authCodeView.textField.text yz_isWithdrawPwdCode]) {
            return YES;
        } else {
            [self registerValidateFail:@"请输入6位数字验证码"];
            return NO;
        }
    }
    
    if (textField == self.passwordView.textField || textField == self.ConfirmPwdView.textField) {
        
        if ([self.passwordView.textField.text yz_isWithdrawPwdCode]) {
            return YES;
        } else {
            [self registerValidateFail:@"提现密码由6位纯数字组成"];
            return NO;
        }
    }
    
    return NO;
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

- (void)resignAllFirstResponder {
    [self.authCodeView.textField resignFirstResponder];
    [self.authCodeView.phoneTextField resignFirstResponder];
    [self.passwordView.textField resignFirstResponder];
    [self.ConfirmPwdView.textField resignFirstResponder];
}

#pragma mark -- UITextFieldDelegate
//点击键盘return
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    if (textField == self.passwordView.textField) {
        [self.ConfirmPwdView.textField becomeFirstResponder];
    } else if (textField == self.ConfirmPwdView.textField) {
        [self.ConfirmPwdView.textField resignFirstResponder];
    }
    
    return YES;
}

- (BOOL)phoneCheck {
    NSInteger phoneNum = [[self.authCodeView.phoneTextField.text substringToIndex:2] integerValue];
    switch (phoneNum) {
        case 13:
        case 14:
        case 15:
        case 17:
        case 18:
            return YES;
            break;
        default:
            return NO;
            break;
    }
}

- (void)textFieldchange:(UITextField *)send {
    
    if ([self.authCodeView.phoneTextField.text length] > 0 &&
        [self.authCodeView.textField.text length] > 0 &&
        [self.passwordView.textField.text length] > 0 &&
        [self.ConfirmPwdView.textField.text length] > 0) {
        _resetPwdButton.layer.borderWidth=0.0;
        _resetPwdButton.enabled = YES;
        return;
    }
    _resetPwdButton.layer.borderWidth=1.0;
    _resetPwdButton.enabled = NO;
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    
    if (textField == self.authCodeView.phoneTextField) {
        if (YZMUserServiceInstance.isValidate) {
            return NO;
        }
    }
    
    return YES;
}

#pragma mark -- registerValid

- (void)registerValidateFail:(NSString *)message {
    
    if (message) {
        [MBProgressHUD showMessage:message];
    }
}

@end
